package com.anysoft.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by wwq on 2017/3/1.
 */

public class FundNewAdapter extends BaseAdapter {

    private Context context;
    private List<QueryFundListBean.DataBean> itemList;
    private LayoutInflater inflater;

    private OnBuyBtnListener onBuyBtnListener;

    public void setOnBuyBtnListener(OnBuyBtnListener onBuyBtnListener) {
        this.onBuyBtnListener = onBuyBtnListener;
    }

    public FundNewAdapter(Context context, List<QueryFundListBean.DataBean> itemList) {
        this.itemList = itemList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        FundNewViewHolder holdler;

        if (convertView == null) {
            holdler = new FundNewViewHolder();
            convertView = inflater.inflate(R.layout.item_fund_new, null);

            holdler.bth_item_new_fund = (Button) convertView
                    .findViewById(R.id.bth_item_new_fund);
            holdler.iv_item_new_fund = (CircleImageView) convertView
                    .findViewById(R.id.iv_item_new_fund);
            holdler.tv_item_new_fund_name = (TextView) convertView
                    .findViewById(R.id.tv_item_new_fund_name);
            holdler.tv_item_new_fund_time = (TextView) convertView
                    .findViewById(R.id.tv_item_new_fund_time);

            convertView.setTag(holdler);
        } else {
            holdler = (FundNewViewHolder) convertView.getTag();
            resetViewHolder(holdler);
        }

        final QueryFundListBean.DataBean bean = itemList.get(position);

        holdler.tv_item_new_fund_name.setText(bean.getFundname());
        if (TextUtils.isEmpty(bean.getCreateTime())) {
            holdler.tv_item_new_fund_time.setText("--");
        } else {
            String[] dates = bean.getCreateTime().split("-");
            String date = dates[0] + "年" + dates[1] + "月" + dates[2] + "日起发行";
            holdler.tv_item_new_fund_time.setText(date);
        }

        holdler.bth_item_new_fund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBuyBtnListener != null) {
                    onBuyBtnListener.OnBuyBtnClick(bean);
                }
            }
        });

        return convertView;
    }

    private void resetViewHolder(FundNewViewHolder holder) {
        holder.iv_item_new_fund.setBackgroundResource(R.mipmap.jijin_xin);
    }

    public interface OnBuyBtnListener {

        /**
         * @param bean
         */
        void OnBuyBtnClick(QueryFundListBean.DataBean bean);
    }
}
